<?php
/**
 * 四六级查分
 * @param string $zkzh
 * @param string $name
 * @param string $method
 * @return array|boolean
 */
function cet_chafen($zkzh,$name,$method="99sushe")
{
	if("99sushe"==$method)
	{
		$result=chafen_99sushe($zkzh, $name);
		if ($result) {
			return $result;
		}else{
			$method="chsi";
		}
	}
	
	if("chsi"==$method)
	{
		$result=chafen_chsi($zkzh, $name);
		if ($result) {
			return $result;
		}else{
			return false;
		}
	}	     
}

/**
 * 99宿舍查分
 * @param string $zkzh
 * @param string $name
 * @return boolean|array
 */
function chafen_99sushe($zkzh,$name)
{
	if($zkzh==null||$name==null)
		return false;
	
	//将名字转码并去前两位
	$name=iconv("UTF-8//IGNORE","GB2312",$name);
	$name=str_split($name,4);
	$name=$name[0];
	
	$url="http://cet.99sushe.com/s";
	$referer="http://cet.99sushe.com";
	$post="id=$zkzh&name=$name";
	$temp=curl($url,$post,$referer);
	$temp=explode(",",$temp);
	if(count($temp)>=8){
		$result=array();
		$result['name']=iconv("GB2312","UTF-8",$temp[7]);
		$result['school']=iconv("GB2312","UTF-8",$temp[6]);
		substr($zkzh,9,1);
		if(substr($zkzh,9,1)==1)
			$result['leveal']="英语四级";
		else
			$result['leveal']="英语六级";
		$result['zkzh']=$zkzh;
		$result['time']=substr($zkzh,6,3);
		$result['zf']=$temp[5];
		$result['tl']=$temp[1];
		$result['yd']=$temp[2];
		$result['zh']=$temp[3];
		$result['xz']=$temp[4];
		return $result;
	}
	else{
		return false;
	}
}

/**
 * 学信网查分
 * @param string $zkzh
 * @param string $name
 * @return boolean
 */
function chafen_chsi($zkzh,$name)
{
	if($zkzh==null||$name==null)
		return false;
	$url="http://www.chsi.com.cn/cet/query?";
	$referer="http://www.chsi.com.cn/";
	$url .= "zkzh=$zkzh&xm=$name";
	$result=curl($url,$post=null,$referer);
	
	//stristr()查找字符串在另一个字符串第一次出现的位置，
	$tem=stristr($result,'<table border="0" align="center" cellpadding="0" cellspacing="6" class="cetTable">');
	$content=substr($tem,0,strpos($tem,'</table>'));
	$content=explode('<tr>',$content);
	for($i=0;$i<count($content,1);$i++)
	{
		$content[$i]='<tr>'.$content[$i];
		$pattern = '/<.*?>/s';
		$replacement = '';
		$content[$i] = preg_replace( $pattern, $replacement, $content[$i]);
		$content[$i]=str_replace("&nbsp;","",$content[$i]);//去&nbsp;
		$content[$i]= trim($content[$i]);// 首先去掉头尾空格
		$content[$i]= preg_replace('/\s(?=\s)/','',$content[$i]);// 接着去掉两个空格以上的
		$content[$i]= preg_replace('/[\n\r\t]/',' ',$content[$i]);// 最后将非空格替换为一个空格
	}
	$temp=$content;
	$result[6];
	//分解成绩
	$patterns = '/\d+/';
	preg_match_all($patterns,$temp[6],$arr);
	for($i=6;$i<11;$i++)
		$temp[$i]=$arr[0][$i-6];
	if($temp[4]==null)
		return false;
	
	$result=array();
	$result['name']=substr($temp[1],10);
	$result['school']=substr($temp[2],10);
	substr($zkzh,9,1);
	if(substr($zkzh,9,1)==1)
		$result['leveal']="英语四级";
	else
		$result['leveal']="英语六级";
	$result['zkzh']=$zkzh;
	$result['time']=substr($zkzh,6,3);
	$result['zf']=$temp[6];
	$result['tl']=$temp[7];
	$result['yd']=$temp[8];
	$result['zh']=$temp[9];
	$result['xz']=$temp[10];
	return $result;
}
/**
 * curl函数
 * @param string $url
 * @param string $post
 * @param string $referer
 * @return boolean|mixed
 */
function curl($url,$post=null,$referer=null)
{
	if($url==null)
		return false;
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_TIMEOUT, 100);
	curl_setopt($curl, CURLOPT_HEADER, 0);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	if($referer){
		curl_setopt($curl, CURLOPT_REFERER, $referer);
	}
	if($post){
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS,$post);
	}
	$result= curl_exec($curl);
	curl_close($curl);
	return $result;
}